package com.briup.jz.web.controller;

import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.service.IUserService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: jz
 * @Author: xjs
 * @Description: 用户控制
 * @Date: 2020/6/12 13:58
 */
@RestController
@RequestMapping("/user")
@Api(description = "用户管理接口")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "添加或更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId" ,value = "2顾客,3员工" ,
                    required = true ,paramType = "query"),  //true代表这个属性必须填
            @ApiImplicitParam(name = "status" ,value = "默认" ,
                    required = false ,paramType = "query")
    })
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(User user){
        userService.saveOrUpdate(user);
        return MessageUtil.success("操作成功");
    }


    @ApiOperation(value = "通过id删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一编号" , required = true,paramType = "query")  //true代表这个属性必须填
    })
    @GetMapping("deleteById")
    public Message deleteById(Long id){
        userService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "查找全部用户及其角色信息")
    @GetMapping("findAllWithRole")
    public Message findAllWithRole(){
        List<UserExtend> list = userService.findAllWithRole();
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "通过id查找用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一编号" , required = true,paramType = "query")  //true代表这个属性必须填
    })
    @GetMapping("findByIdWithRole")
    public Message findByIdWithRole(Long id){
        UserExtend userExtend = userService.findByIdWithRole(id);
        return MessageUtil.success(userExtend);
    }

//    @ApiOperation(value = "通过id修改用户状态，正常修改为禁用，禁用修改为正常")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id" ,value = "唯一编号" , required = true,paramType = "query")  //true代表这个属性必须填
//    })
//    @GetMapping("updateStatus")
//    public Message updateStatusToForbidden(Long id){
//        userService.updateStatus(id);
//        return MessageUtil.success("修改成功");
//    }

    @ApiOperation(value = "通过id修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一编号" , required = true,paramType = "query"), //true代表这个属性必须填
            @ApiImplicitParam(name = "userFace" ,value = "用户头像" ,required = true , paramType = "query")
    })
    @GetMapping("updateUserFace")
    public Message updateUserFace(Long id , String userFace){
        userService.updateUserFace(id, userFace);
        return MessageUtil.success("更新成功");
    }

    @ApiOperation(value = "检查用户权限")
    @GetMapping("checkPrivilege")
    public Message checkPrivilege(Long id , String privilege){
        userService.checkPrivilege(id, privilege);
        return MessageUtil.success("拥有权限");
    }

    @ApiOperation(value = "查找所有员工")
    @GetMapping("findAllEmployee")
    public Message findAllEmployee(){
        return MessageUtil.success(userService.findAllEmployee());
    }

    @ApiOperation(value = "查询状态为正常的员工")
    @GetMapping("findAllEmployeeAndStatus")
    public Message findAllEmployeeAndStatus(){
        return MessageUtil.success(userService.findAllEmployeeAndStatus());
    }

    @ApiOperation(value = "根据姓名模糊查找用户")
    @GetMapping("findUserByName")
    public Message findUserByName(String name){
        return MessageUtil.success(userService.findUserByName(name));
    }
}
